
// Open MoneySpinner Suite v1
// An open source business management software system written in Java and MySQL
// Recommended IDE is NetBeans IDE 7.0.1
// Support Web Site: http://www.milliscript.com
//
// Copyright (C) 2014, Abiodun Aremu, Ibadan/NIGERIA.
// Open MoneySpinner Suite is distributed under the terms of the Apache License version 2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package openmoneyspinnersuite;

/**
 *
 * @author Abiodun Aremu
 */

import javax.swing.*;
import java.sql.*;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.text.BadLocationException;
import javax.swing.text.ChangedCharSetException;
import javax.swing.text.Document;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import java.util.StringTokenizer;

final class ViewMerchant extends javax.swing.JInternalFrame {

    Object[][] tableObject=new Object[][]{};
    Systems systems=new Systems();
    PreparedStatement statement;
    private Document loader;
    String phoneNumbers="";
    Thread processThread=new Thread();
    Thread titleThread=new Thread();
    /** Creates new form ViewCredit */
     ViewMerchant() {
        initComponents();
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            tableObject,
            new String [] {
                "Check","Merchant ID", "Name" , "Group(s)"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Boolean.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class
            };
        
            boolean[] canEdit = new boolean [] {
                true,false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        /* Execute thread */
        processThread=new Thread(new Runnable() {
            public void run() {
            disableControls();
            refreshTable("");
            resetGroup();
            resetTransMonth();
            resetTransYear();
            }            
        });
        titleThread=new Thread(new Runnable() {
            public void run() {
                while(processThread.isAlive())setProcessingTitle(titleThread);
                enableControls();
                System.out.println("viewItemThread stopped runing.");
            }
        });
        processThread.start();
        titleThread.start();
     }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jGroupComboBox = new javax.swing.JComboBox();
        jCheckBox1 = new javax.swing.JCheckBox();
        jScrollPane2 = new javax.swing.JScrollPane();
        jSendToTextArea = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        transMonthComboBox = new javax.swing.JComboBox();
        transDayComboBox = new javax.swing.JComboBox();
        transYearComboBox = new javax.swing.JComboBox();
        jCheckBox2 = new javax.swing.JCheckBox();
        jSpinner1 = new javax.swing.JSpinner();
        jSenderTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jMessageTextArea = new javax.swing.JTextArea();
        jLabel9 = new javax.swing.JLabel();
        jSendMessageButton = new javax.swing.JButton();
        jCancelButton = new javax.swing.JButton();
        jRefreshButton = new javax.swing.JButton();
        jRemoveGroupButton = new javax.swing.JButton();
        jAddGroupButton = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(openmoneyspinnersuite.OpenMS_Finance_App.class).getContext().getResourceMap(ViewMerchant.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setToolTipText(resourceMap.getString("Form.toolTipText")); // NOI18N
        setFrameIcon(resourceMap.getIcon("Form.frameIcon")); // NOI18N
        setName("Form"); // NOI18N
        setVisible(true);

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Check", "Merchant ID", "Name", "Group(s)"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Boolean.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                true, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setName("jTable1"); // NOI18N
        jTable1.getTableHeader().setReorderingAllowed(false);
        jTable1.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jTable1PropertyChange(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setMinWidth(40);
        jTable1.getColumnModel().getColumn(0).setPreferredWidth(40);
        jTable1.getColumnModel().getColumn(0).setMaxWidth(40);
        jTable1.getColumnModel().getColumn(1).setPreferredWidth(10);
        jTable1.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("jTable1.columnModel.title0")); // NOI18N
        jTable1.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("jTable1.columnModel.title2")); // NOI18N
        jTable1.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("jTable1.columnModel.title16")); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel1.border.title"))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        jGroupComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-- Group --" }));
        jGroupComboBox.setName("jGroupComboBox"); // NOI18N
        jGroupComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jGroupComboBoxActionPerformed(evt);
            }
        });

        jCheckBox1.setText(resourceMap.getString("jCheckBox1.text")); // NOI18N
        jCheckBox1.setName("jCheckBox1"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jSendToTextArea.setColumns(20);
        jSendToTextArea.setRows(5);
        jSendToTextArea.setName("jSendToTextArea"); // NOI18N
        jScrollPane2.setViewportView(jSendToTextArea);

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        transMonthComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-- Month --" }));
        transMonthComboBox.setEnabled(false);
        transMonthComboBox.setName("transMonthComboBox"); // NOI18N
        transMonthComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                transMonthComboBoxActionPerformed(evt);
            }
        });

        transDayComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-- Day --" }));
        transDayComboBox.setEnabled(false);
        transDayComboBox.setName("transDayComboBox"); // NOI18N

        transYearComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-- Year --" }));
        transYearComboBox.setEnabled(false);
        transYearComboBox.setName("transYearComboBox"); // NOI18N

        jCheckBox2.setText(resourceMap.getString("jCheckBox2.text")); // NOI18N
        jCheckBox2.setName("jCheckBox2"); // NOI18N
        jCheckBox2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox2ActionPerformed(evt);
            }
        });

        jSpinner1.setEnabled(false);
        jSpinner1.setName("jSpinner1"); // NOI18N

        jSenderTextField.setText(resourceMap.getString("jSenderTextField.text")); // NOI18N
        jSenderTextField.setName("jSenderTextField"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        jMessageTextArea.setColumns(20);
        jMessageTextArea.setRows(5);
        jMessageTextArea.setName("jMessageTextArea"); // NOI18N
        jScrollPane3.setViewportView(jMessageTextArea);

        jLabel9.setText(resourceMap.getString("jLabel9.text")); // NOI18N
        jLabel9.setName("jLabel9"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(66, 66, 66)
                        .addComponent(jCheckBox1))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(26, 26, 26)
                        .addComponent(jSenderTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 304, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 515, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(64, 64, 64)
                        .addComponent(jGroupComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8)
                            .addComponent(jLabel9))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 408, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(transMonthComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(transDayComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(transYearComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jCheckBox2)))))
                .addContainerGap(84, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jSenderTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, 0, 0, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addComponent(jLabel1)
                        .addGap(35, 35, 35)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jGroupComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jCheckBox2)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(transMonthComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(transDayComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(transYearComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel8)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jCheckBox1))
        );

        jSendMessageButton.setIcon(resourceMap.getIcon("jSendMessageButton.icon")); // NOI18N
        jSendMessageButton.setText(resourceMap.getString("jSendMessageButton.text")); // NOI18N
        jSendMessageButton.setToolTipText(resourceMap.getString("jSendMessageButton.toolTipText")); // NOI18N
        jSendMessageButton.setName("jSendMessageButton"); // NOI18N
        jSendMessageButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jSendMessageButtonActionPerformed(evt);
            }
        });

        jCancelButton.setFont(resourceMap.getFont("jCancelButton.font")); // NOI18N
        jCancelButton.setIcon(resourceMap.getIcon("jCancelButton.icon")); // NOI18N
        jCancelButton.setText(resourceMap.getString("jCancelButton.text")); // NOI18N
        jCancelButton.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jCancelButton.setName("jCancelButton"); // NOI18N
        jCancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCancelButtonActionPerformed(evt);
            }
        });

        jRefreshButton.setFont(resourceMap.getFont("jRefreshButton.font")); // NOI18N
        jRefreshButton.setIcon(resourceMap.getIcon("jRefreshButton.icon")); // NOI18N
        jRefreshButton.setText(resourceMap.getString("jRefreshButton.text")); // NOI18N
        jRefreshButton.setToolTipText(resourceMap.getString("jRefreshButton.toolTipText")); // NOI18N
        jRefreshButton.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jRefreshButton.setName("jRefreshButton"); // NOI18N
        jRefreshButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRefreshButtonActionPerformed(evt);
            }
        });

        jRemoveGroupButton.setIcon(resourceMap.getIcon("jRemoveGroupButton.icon")); // NOI18N
        jRemoveGroupButton.setText(resourceMap.getString("jRemoveGroupButton.text")); // NOI18N
        jRemoveGroupButton.setToolTipText(resourceMap.getString("jRemoveGroupButton.toolTipText")); // NOI18N
        jRemoveGroupButton.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jRemoveGroupButton.setName("jRemoveGroupButton"); // NOI18N
        jRemoveGroupButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRemoveGroupButtonActionPerformed(evt);
            }
        });

        jAddGroupButton.setIcon(resourceMap.getIcon("jAddGroupButton.icon")); // NOI18N
        jAddGroupButton.setText(resourceMap.getString("jAddGroupButton.text")); // NOI18N
        jAddGroupButton.setToolTipText(resourceMap.getString("jAddGroupButton.toolTipText")); // NOI18N
        jAddGroupButton.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jAddGroupButton.setName("jAddGroupButton"); // NOI18N
        jAddGroupButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jAddGroupButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 76, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jSendMessageButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jCancelButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jRemoveGroupButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jAddGroupButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jRefreshButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 894, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jAddGroupButton, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jRemoveGroupButton, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jRefreshButton, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jCancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jSendMessageButton, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(36, 36, 36))
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jRefreshButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRefreshButtonActionPerformed

        /* Execute thread */
        processThread=new Thread(new Runnable() {
            public void run() {
            disableControls();
                if(jCheckBox1.isSelected()){
                refreshTable(jGroupComboBox.getSelectedItem().toString());
                }
                else{
                refreshTable("");
                }
            }
        });
        titleThread=new Thread(new Runnable() {
            public void run() {
                while(processThread.isAlive())setProcessingTitle(titleThread);
                enableControls();
                System.out.println("viewItemThread stopped runing.");
            }
        });
        processThread.start();
        titleThread.start();
}//GEN-LAST:event_jRefreshButtonActionPerformed

    private void jCancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCancelButtonActionPerformed
        
        if(processThread.isAlive())
        {
            try{
                processThread.stop();
                titleThread.stop();
                System.out.println("Threads successfully stopped.");
            }catch(Exception e){System.out.println("Thread stopping error: "+e);}
            this.setTitle("::. View Merchant");
            enableControls();
        }
        else
        dispose();
}//GEN-LAST:event_jCancelButtonActionPerformed
    private void disableControls()
    {
        jRefreshButton.setEnabled(false);
        jAddGroupButton.setEnabled(false);
        jRemoveGroupButton.setEnabled(false);
        jSendMessageButton.setEnabled(false);
        jCheckBox1.setEnabled(false);
        jCheckBox2.setEnabled(false);
        transDayComboBox.setEnabled(false);
        transMonthComboBox.setEnabled(false);
        transYearComboBox.setEnabled(false);
    }                                             
    private void enableControls()
    {
        jRefreshButton.setEnabled(true);
        jAddGroupButton.setEnabled(true);
        jRemoveGroupButton.setEnabled(true);
        jSendMessageButton.setEnabled(true);
        jCheckBox1.setEnabled(true);
        jCheckBox2.setEnabled(true);
        if(jCheckBox2.isSelected())
        {
            transDayComboBox.setEnabled(true);
            transMonthComboBox.setEnabled(true);
            transYearComboBox.setEnabled(true);
        }
        this.setTitle("::. View Merchant");
    }
     
private void jAddGroupButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jAddGroupButtonActionPerformed

        /* Execute thread */
        processThread=new Thread(new Runnable() {
            public void run() {
            disableControls();
            addGroupButtonClicked();
            }
        });
        titleThread=new Thread(new Runnable() {
            public void run() {
                while(processThread.isAlive())setProcessingTitle(titleThread);
                enableControls();
                System.out.println("viewItemThread stopped runing.");
            }
        });
        processThread.start();
        titleThread.start();
}//GEN-LAST:event_jAddGroupButtonActionPerformed
private void addGroupButtonClicked()
{
                int row=jTable1.getSelectedRow();
                if(jGroupComboBox.getSelectedItem().equals("-- Group --"))
                {
                    JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"PLEASE SELECT A GROUP ","ERROR !",JOptionPane.ERROR_MESSAGE);
                    return;
                }    
                else if(row==-1)
                {
                    JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"PLEASE SELECT A MERCHANT ","ERROR !",JOptionPane.ERROR_MESSAGE);
                    return;
                }          
                else{    
                submitData();
                refreshTable("");
                }
}
private void jGroupComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jGroupComboBoxActionPerformed

                if(jGroupComboBox.getSelectedItem().equals("-- Group --"))
                {
                    resetGroup();
                }    
}//GEN-LAST:event_jGroupComboBoxActionPerformed

private void jRemoveGroupButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRemoveGroupButtonActionPerformed

        /* Execute thread */
        processThread=new Thread(new Runnable() {
            public void run() {
            disableControls();
            removeGroupButton();
            }
        });
        titleThread=new Thread(new Runnable() {
            public void run() {
                while(processThread.isAlive())setProcessingTitle(titleThread);
                enableControls();
                System.out.println("viewItemThread stopped runing.");
            }
        });
        processThread.start();
        titleThread.start();
}//GEN-LAST:event_jRemoveGroupButtonActionPerformed
private void removeGroupButton()
{
            int row=jTable1.getSelectedRow();
            if(jGroupComboBox.getSelectedItem().equals("-- Group --"))
            {
                JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"PLEASE SELECT A GROUP ","ERROR !",JOptionPane.ERROR_MESSAGE);
                return;
            }
            else if(row==-1)
            {
                JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"PLEASE SELECT A MERCHANT ","ERROR !",JOptionPane.ERROR_MESSAGE);
                return;
            }        
            else{    
            deleteData();
            refreshTable("");
            }
}
private void transMonthComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_transMonthComboBoxActionPerformed
        // TODO add your handling code here:
        String transMonth=transMonthComboBox.getSelectedItem().toString();

        if(transMonth.equals("-- Month --")) {
        } else {
            resetTransDay(transMonth);
        }
}//GEN-LAST:event_transMonthComboBoxActionPerformed

private void jCheckBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox2ActionPerformed
    if(jCheckBox2.isSelected())
    {
        transDayComboBox.setEnabled(true);
        transMonthComboBox.setEnabled(true);
        transYearComboBox.setEnabled(true);
        jSpinner1.setEnabled(true);
    }else{
        transDayComboBox.setEnabled(false);
        transMonthComboBox.setEnabled(false);
        transYearComboBox.setEnabled(false);
        jSpinner1.setEnabled(false);  
        resetTransMonth();
        resetTransYear();
        jSpinner1.setValue("0");  
    }
    
}//GEN-LAST:event_jCheckBox2ActionPerformed

private void jSendMessageButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jSendMessageButtonActionPerformed
    String query="a";
    phoneNumbers=jSendToTextArea.getText().trim();
    if(jSenderTextField.getText().trim().isEmpty())
    {
        JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"PLEASE ENTER A SENDER NAME !","ERROR !",JOptionPane.ERROR_MESSAGE);
        return;
    }
    else if(jMessageTextArea.getText().trim().isEmpty())
    {
        JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"PLEASE ENTER A MESSAGE !","ERROR !",JOptionPane.ERROR_MESSAGE);
        return;
    }
    else if(jSendToTextArea.getText().trim().isEmpty()&&jGroupComboBox.getSelectedItem().toString().equalsIgnoreCase("-- Group --"))
    {
        JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"PLEASE ENTER RECEIVER NUMBER(S) !","ERROR !",JOptionPane.ERROR_MESSAGE);
        return;
    }
    else if(OpenMSApp.bSMSusername=="bSMSInvalidUsername")
    {
        JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"PLEASE LOGIN YOUR BULK SMS ACCOUNT !","ERROR !",JOptionPane.ERROR_MESSAGE);
        return;
    }
    else if(OpenMSApp.bSMSpassword=="bSMSInvalidPassword")
    {
        JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"PLEASE LOGIN YOUR BULK SMS ACCOUNT !","ERROR !",JOptionPane.ERROR_MESSAGE);
        return;
    }
    else if(jCheckBox2.isSelected())
    {
        if(transMonthComboBox.getSelectedItem().equals("-- Month --"))
        {
            JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"PLEASE SELECT TRANSACTION'S DATE MONTH ","ERROR !",JOptionPane.ERROR_MESSAGE);
            return;
        }
        if(transDayComboBox.getSelectedItem().equals("-- Day --"))
        {
            JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"PLEASE SELECT TRANSACTION'S DATE DAY ","ERROR !",JOptionPane.ERROR_MESSAGE);
            return;
        }
        if(transYearComboBox.getSelectedItem().equals("-- Year --"))
        {
            JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"PLEASE SELECT TRANSACTION'S DATE YEAR ","ERROR !",JOptionPane.ERROR_MESSAGE);
            return;
        }
    }
    InputStream in = null;
    try
    {
//            File file=new File("http://localhost/cgi-cgi-bin/CgiWriteSpt.cgi");
//            FileReader fd=new FileReader(file);
//            int io=(int)file.length();
//            char[] cbkuf=new char[io];
//            int i=fd.read(cbkuf);
//        System.out.println("read from page: "+String.valueOf(cbkuf));
            
        if(systems.isStringBulkSMSFormat(phoneNumbers)==false){return;}
        //"http://localhost/cgi-cgi-bin/CgiWriteSpt.cgi"
        URL api=new URL("http://www.smslive247.com/http/index.aspx?cmd=sendquickmsg&owneremail=abiodunaremu@milliscript.com"+
                "&subacct="+OpenMSApp.bSMSusername+"&subacctpwd="+OpenMSApp.bSMSpassword+"&msg="+jMessageTextArea.getText().trim()
                +"&sender="+jSenderTextField.getText().trim()+"&sendto="+phoneNumbers+"&msgtype=0");
        ///query=api.getQuery();
        // Connect and run api
        URLConnection urlc = api.openConnection();
        in = urlc.getInputStream();
        in.close();        
        JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"MESSAGE SENT SUCCESSFULLY ","SUCCESSFUL !",JOptionPane.INFORMATION_MESSAGE);

    }
    catch(Exception e){
        JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"UNABLE TO SEND MESSAGE ","ERROR !",JOptionPane.ERROR_MESSAGE);

        System.out.println("Unable to send message: "+e);
    }
}//GEN-LAST:event_jSendMessageButtonActionPerformed

private void jTable1PropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jTable1PropertyChange

    //System.out.println(jTable1.getColumnModel().getColumn(0).getWidth());
}//GEN-LAST:event_jTable1PropertyChange
 
                   
    private void setProcessingTitle(Thread thread)
    {
        try{
            this.setTitle("::. View Merchant - Processing Request");
            thread.sleep(500);
            this.setTitle("::. View Merchant - Processing Request.");
            thread.sleep(500);
            this.setTitle("::. View Merchant - Processing Request..");
            thread.sleep(500);
            this.setTitle("::. View Merchant - Processing Request...");
            thread.sleep(500);
        }catch(Exception e){System.out.println("Title thread"+e);}
    }
     void resetTransYear()
    {
        String[] year=systems.getYears(1920);
        year[0]="-- Year --";
        transYearComboBox.setModel(new DefaultComboBoxModel(year));
    }
     void resetTransMonth()
    {
        String[] month=systems.getMonthArray();
        month[0]="-- Month --";

        transMonthComboBox.setModel(new DefaultComboBoxModel(month));

    }
     void resetTransDay(String Month)
    {
        String[] day={"-- Day --"};
        if(Month.isEmpty())
        {
            transDayComboBox.setModel(new DefaultComboBoxModel(day));
        }
        else
        {
            day=systems.getDays(Month);
            day[0]="-- Day --";
            transDayComboBox.setModel(new DefaultComboBoxModel(day));
        }

    }
     void submitData()
    {
        int row=jTable1.getSelectedRow();
        //Connect.createMSSQLConnection();
        Connect.changeDB(OpenMSApp.Database_A);
        statement=Connect.createStatement(Connect.procInit+" prnInsMerchantGroup"+Connect.procInitStart+"?,?"+Connect.procInitEnd);
            try{
                statement.setString(1,jGroupComboBox.getSelectedItem().toString().trim());
                statement.setString(2,jTable1.getValueAt(row,2).toString().trim());
            }
            catch(Exception e)
            {
                System.out.println("ViewMerchant.submitData() gave error ! "+e);
            }
        Connect.alert="Y";
        Connect.executeUpdateStatement(statement);
        Connect.closeConnection();
    }
     void deleteData()
    {
        int row=jTable1.getSelectedRow();
        //Connect.createMSSQLConnection();
        Connect.changeDB(OpenMSApp.Database_A);
        statement=Connect.createStatement(Connect.procInit+" prnDeleteMerchantGroup"+Connect.procInitStart+"?,?"+Connect.procInitEnd);
            try{
                statement.setString(1,jGroupComboBox.getSelectedItem().toString().trim());
                statement.setString(2,jTable1.getValueAt(row,1).toString().trim());
            }
            catch(Exception e)
            {
                System.out.println("ViewMerchant.DeleteData() gave error ! "+e);
            }
        Connect.alert="Y";
        Connect.executeUpdateStatement(statement);
        Connect.closeConnection();
    }
     void print()
    {
        try
        {
            jTable1.print();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    void reset()
    {
        refreshTable("");
        resetGroup();
        resetTransMonth();
        resetTransYear(); 
        resetTransDay("");
        jSenderTextField.setText("");
        jMessageTextArea.setText("");
        jSendToTextArea.setText("");
        jCheckBox2.setSelected(false);
    }
    void resetCombo(){resetGroup();}
     void resetGroup()
    {
        String[] group=systems.getTableDataArray(OpenMSApp.Database_A,"Groups",2);
        group[0]="-- Group --";
        jGroupComboBox.setModel(new DefaultComboBoxModel(group));
        jCheckBox1.setSelected(false);
    }
     void refreshTable(String searchPara)
    {
            String[] dbMerchantID=null;
            String[] dbName=null;
            String[] dbGroup=null;
            if(searchPara.isEmpty() && jCheckBox1.isSelected()==false)
            {
                dbMerchantID=systems.getTableDataArrayNum(OpenMSApp.Database_A, "vwMerchant",1);
                dbName=systems.getTableDataArrayNum(OpenMSApp.Database_A, "vwMerchant",2);
                //String[] dbAccountBalance=systems.getTableDataArrayNum(OpenMSApp.Database_A, "vwMerchant",3);

                dbGroup=new String[dbMerchantID.length];
                for(int x=0;x<dbMerchantID.length;x++)
                {
                    String merchantGroup="";
                    String[] dbGroupUnit=systems.getColumn(OpenMSApp.Database_A, "Select * from MerchantGroup where cMerchantID='"+dbMerchantID[x]+"'", 1);
                    for(int y=0;y<dbGroupUnit.length;y++)
                    {   
                        merchantGroup+=systems.getValue(OpenMSApp.Database_A, "Select * from Groups where cGroupCode = '"+dbGroupUnit[y]+"'", 2)+" | ";
                    }
                    dbGroup[x]=merchantGroup;
                }
            }
            else if(searchPara.isEmpty()==false && jCheckBox1.isSelected()==false)
            {
                //building up search query
                String multipleSearch[]=searchPara.split(",");
                String queryPara="where (cMerchantID = '"+multipleSearch[0]+"') or (vName = '"+multipleSearch[0]+"') ";//or (cPhone like '%"+multipleSearch[0]+"%') ";
                
                for(int k=1;k<multipleSearch.length;k++)
                {
                    queryPara+="or (cMerchantID like '%"+multipleSearch[k]+"%') or (vName like '%"+multipleSearch[k]+"%') ";//or (cPhone like '%"+multipleSearch[k]+"%') ";
                }

                dbMerchantID=systems.getColumn(OpenMSApp.Database_A, "Select * from vwMerchant "+queryPara, 1);
                dbName=systems.getColumn(OpenMSApp.Database_A, "Select * from vwMerchant "+queryPara, 2);
                dbGroup=dbGroup=new String[dbMerchantID.length];
                
                for(int x=0;x<dbMerchantID.length;x++)
                {
                    String merchantGroup="";
                    String[] dbGroupUnit=systems.getColumn(OpenMSApp.Database_A, "Select * from MerchantGroup where cMerchantID='"+dbMerchantID[x]+"'", 1);
                    for(int y=0;y<dbGroupUnit.length;y++)
                    {   
                        merchantGroup+=systems.getValue(OpenMSApp.Database_A, "Select * from Groups where cGroupCode = '"+dbGroupUnit[y]+"'", 2)+" | ";
                    }
                    dbGroup[x]=merchantGroup;
                }
            }
            else if(jCheckBox1.isSelected()==true)
            {
                if(jGroupComboBox.getSelectedItem().equals("-- Group --"))
                {
                    JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"PLEASE SELECT A GROUP ","ERROR !",JOptionPane.ERROR_MESSAGE);
                    return;
                }        
                
                String groupCode=systems.getValue(OpenMSApp.Database_A, "Select * from groups where vName='"+searchPara+"'",1);
                String queryPara="where (cMerchantID IN (Select cMerchantID from MerchantGroup where cGroupCode='"+groupCode+"')) ";// or (cGroupCode IN ( select cGroupCode from groups where cMerchantID='"+multipleSearch[0]+"')) ";//or (cPhone like '%"+multipleSearch[0]+"%') ";
                
                dbMerchantID=systems.getColumn(OpenMSApp.Database_A, "Select * from vwMerchant "+queryPara, 1);
                dbName=systems.getColumn(OpenMSApp.Database_A, "Select * from vwMerchant "+queryPara, 2);
                dbGroup=dbGroup=new String[dbMerchantID.length];
                
                for(int x=0;x<dbMerchantID.length;x++)
                {
                    String merchantGroup="";
                    String[] dbGroupUnit=systems.getColumn(OpenMSApp.Database_A, "Select * from MerchantGroup where cMerchantID='"+dbMerchantID[x]+"'", 1);
                    for(int y=0;y<dbGroupUnit.length;y++)
                    {   
                        merchantGroup+=systems.getValue(OpenMSApp.Database_A, "Select * from Groups where cGroupCode='"+dbGroupUnit[y]+"'", 2)+" | ";
                    }
                    dbGroup[x]=merchantGroup;
                }
            }

        int x=0;
        int y=dbMerchantID.length;

        tableObject=new Object[y][4];
        while(x<y)
        {
            tableObject[x][1]=dbMerchantID[x].trim();
            tableObject[x][2]=dbName[x].trim();
            try
            {
                tableObject[x][3]=dbGroup[x];
            }
            catch(Exception e)
            {
                tableObject[x][3]="Er!M1C01K0";
            }
            systems.freeHeap();
            x+=1;
            if(x==y){break;}
       }
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            tableObject,
            new String [] {
                "Check","Merchant ID", "Name" , "Group(s)"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Boolean.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class
            };
        
            boolean[] canEdit = new boolean [] {
                true,false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });

        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setMinWidth(40);
        jTable1.getColumnModel().getColumn(0).setPreferredWidth(40);
        jTable1.getColumnModel().getColumn(0).setMaxWidth(40);
        jTable1.getColumnModel().getColumn(1).setPreferredWidth(10);
        resetGroup();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jAddGroupButton;
    private javax.swing.JButton jCancelButton;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JComboBox jGroupComboBox;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextArea jMessageTextArea;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jRefreshButton;
    private javax.swing.JButton jRemoveGroupButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JButton jSendMessageButton;
    private javax.swing.JTextArea jSendToTextArea;
    private javax.swing.JTextField jSenderTextField;
    private javax.swing.JSpinner jSpinner1;
    private javax.swing.JTable jTable1;
    private javax.swing.JComboBox transDayComboBox;
    private javax.swing.JComboBox transMonthComboBox;
    private javax.swing.JComboBox transYearComboBox;
    // End of variables declaration//GEN-END:variables

}
